<script>
  const { checkUpdate } = require('@/util/util')
  const { loginApi } = require('@/api/')
  
  export default {
    onLaunch: function() {
      console.log('App Launch')
      checkUpdate()
      // this.checkLogin()
    },
    onShow: function(options) {
      console.log('App Show')
      console.log('options ===', options)
      if (options.query && options.query.qrCode) {
        let scene = decodeURIComponent(options.query.qrCode);
        let temp = scene.split('?')[0].split('/');
        let store_id = temp[temp.length - 1]
        uni.setStorageSync('store_id', store_id)
      }
    },
    onHide: function() {
      console.log('App Hide')
    },
    methods: {
      checkLogin(callback) {
        if (uni.getStorageSync('token')) {
          // 有Token
          // uni.reLaunch({
          //   url: '/pages/index/index'
          // })
          typeof callback === 'function' && callback()
        } else {
          // 无Token
          if (uni.getStorageSync('open_id')) {
            // 有open_id
            // uni.reLaunch({
            //   url: '/pages/bind/bind'
            // })
            typeof callback === 'function' && callback('/pages/bind/bind')
          } else {
            // 无open_id
            this.login(callback)
          }
        }
      },
      login() {
        uni.login({
          success: (res) => {
            loginApi({
              code: res.code
            }).then(resu => {
              const { code, open_id, token } = resu
              if (code === 30000) {
                // 要绑定手机号
                if (open_id) {
                  uni.setStorageSync('open_id', open_id)
                  // uni.reLaunch({
                  //   url: '/pages/bind/bind'
                  // })
                  typeof callback === 'function' && callback('/pages/bind/bind')
                } else {
                  // 重新获取code
                  this.login()
                }
              } else {
                // 不用绑定手机号
                if (token) {
                  uni.setStorageSync('token', token)
                  typeof callback === 'function' && callback()
                } else {
                  // 重新获取code
                  this.login()
                }
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  /*每个页面公共css */
  @import url("/static/css/public.css");
  
  image,
  input,
  textarea {
    display: block;
  }
  
  input {
    min-height: 0;
    height: auto;
  }
</style>
